package com.example.bbgds.test.wifiap;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.example.bbgds.test.R;

public class StartActivity extends Activity {
	private static String TAG = "StartActivity";

	private final String defaultHotName = "meizu";
	private final String defaultHotPwd = "15399973503";
	
	private boolean isWifiOpen = false;
	private WifiHotUtil wifiHotUtil;
	private Button btnWifiHot;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wifiap);
		
		//start WiFiAPService
		WiFiAPService.startService(this);
		//init WifiHotUtil
		wifiHotUtil = new WifiHotUtil(this);
		
		btnWifiHot = (Button)findViewById(R.id.btnOpenWifiHot);
		btnWifiHot.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (!isWifiOpen) {
					openWifiHot();
				}else {
					closeWifiHot();
				}
			}
		});
		
		WiFiAPService.addWiFiAPListener(new WiFiAPListener() {
			
			@Override
			public void stateChanged(int state) {
				Log.i(TAG, "state= "+state);
                switch (state) {
				case WiFiAPListener.WIFI_AP_OPENING:
					btnWifiHot.setText(getResources().getString(R.string.opening_wifi_hot));
					break;
				case WiFiAPListener.WIFI_AP_OPEN_SUCCESS:
					isWifiOpen = true;
					Toast.makeText(StartActivity.this, "WiFi热点"+defaultHotName+"已开启！", 
							Toast.LENGTH_LONG).show();
					btnWifiHot.setText(getResources().getString(R.string.close_wifi_hot));
					break;
				case WiFiAPListener.WIFI_AP_CLOSEING:
					btnWifiHot.setText(getResources().getString(R.string.closeing_wifi_hot));
					break;
				case WiFiAPListener.WIFI_AP_CLOSE_SUCCESS:
					isWifiOpen = false;
					Toast.makeText(StartActivity.this, "WiFi热点"+defaultHotName+"已关闭！", 
							Toast.LENGTH_LONG).show();
					btnWifiHot.setText(getResources().getString(R.string.open_wifi_hot));
					break;
				default:
					break;
				}
			}
		});
		
	}
	
	
	/**
	 * open the wifi hot with the setted hotName and password
	 */
	private void openWifiHot() {
		wifiHotUtil.startWifiAp(defaultHotName, defaultHotPwd);
	}
	
	/**
	 * close wifi hot
	 */
	private void closeWifiHot() {
		wifiHotUtil.closeWifiAp();
	}
	
}
